package com.easy.app.controller.system;

import com.easy.framework.cache.model.Dict;
import com.easy.framework.common.model.R;
import com.easy.framework.common.validate.Add;
import com.easy.framework.common.validate.Update;
import com.easy.framework.database.page.Pagination;
import com.easy.framework.poi.utils.ExcelUtils;
import com.easy.framework.web.base.BaseController;
import com.easy.module.log.annotation.Log;
import com.easy.module.log.enums.Operation;
import com.easy.module.system.entity.SysDictData;
import com.easy.module.system.query.SysDictDataQuery;
import com.easy.module.system.service.ISysDictDataService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author Easy
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController
{
    private final ISysDictDataService dictDataService;

    @PreAuthorize("@perm.hasPermission('system:dict$list')")
    @GetMapping("/page")
    public R<Pagination<SysDictData>> list(SysDictDataQuery query)
    {
        return R.success(dictDataService.page(query));
    }

    @Log(title = "字典数据", type = Operation.EXPORT)
    @PreAuthorize("@perm.hasPermission('system:dict$export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictDataQuery query) throws IOException {
        List<SysDictData> list = dictDataService.list(query);
        ExcelUtils.exportExcel(response, list, "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@perm.hasPermission('system:dict$query')")
    @GetMapping(value = "/{id}")
    public R<SysDictData> get(@PathVariable("id") Long id)
    {
        return R.success(dictDataService.getById(id));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/code/{dictType}")
    public R<List<Dict>> dictType(@PathVariable("dictType") String dictType)
    {
        return R.success(dictDataService.getDictionaryCache(dictType));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@perm.hasPermission('system:dict$add')")
    @Log(title = "字典数据", type = Operation.INSERT)
    @PostMapping
    public R add(@Validated({Add.class}) @RequestBody SysDictData dict)
    {
        return R.result(dictDataService.add(dict));
    }

    /**
     * 修改保存字典类型
     */
    @PreAuthorize("@perm.hasPermission('system:dict$edit')")
    @Log(title = "字典数据", type = Operation.UPDATE)
    @PutMapping
    public R edit(@Validated({Update.class}) @RequestBody SysDictData dict)
    {
        return R.result(dictDataService.update(dict));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@perm.hasPermission('system:dict$remove')")
    @Log(title = "字典类型", type = Operation.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable("ids") Long[] ids)
    {
        dictDataService.deleteByIds(ids);
        return R.success();
    }
}
